# 2 给定一组数据网址数据，请判断这些网址是否可以访问； 用多线程的方式来实现；
#    请查资料，Python的 requests库，如何判断一个网址可以访问；

import requests
import threading


def getHtmlText(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return "产生异常"


i = 0


def fun2():
    with open("c:\\url_data .txt","r") as f:
        lines = f.readlines()
        global i
        while i < len(lines):
            print(getHtmlText(lines[i]))
            mutex.acquire()
            i = i + 1
            mutex.release()

mutex = threading.Lock()

if __name__ == "__main__":
    from concurrent.futures import ThreadPoolExecutor

    threadPool = ThreadPoolExecutor(max_workers=5)
    for i in range(0, 5):
        future = threadPool.submit(fun2())
    threadPool.shutdown(wait=True)
